「 Rust 」快速开始

「 Rust 」快速开始


简介

Rust 是由 Mozilla 主导开发的通用、编译型编程语言。设计准则为“安全、并发、实用”,支持函数式、并发式、过程式以及面向对象的编程风格。
Rust 语言原本是 Mozilla 员工 Graydon Hoare 的私人计划,而 Mozilla 于 2009 年开始赞助这个计划,并且在 2010 年首次公开。也在同一年,其编译器源代码开始由原本的 OCaml 语言转移到用 Rust 语言,进行 bootstrapping 工作,称做 “rustc”,并于 2011 年实际完成。这个可自我编译的编译器在架构上采用了 LLVM 做为它的后端。
第一个有版本号的 Rust 编译器于 2012 年 1 月发布。Rust 1.0 是第一个稳定版本,于 2015 年 5 月 15 日发布。
Rust 是在完全开放的情况下进行开发,并且相当欢迎社区的反馈。在 1.0 稳定版之前,语言设计也因为透过撰写 Servo 网页浏览器排版引擎和 rustc 编译器本身,而有进一步的改善。虽然它由 Mozilla 资助,但它其实是一个共有项目,有很大部分的代码是来自于社区的贡献者。

安装

Rust 官网

https://www.rust-lang.org

Rustup 安装

Rustup 是一个针对 Rust 语言的工具链管理器(toolchain manager),其目标是让交叉编译 Rust 代码更加简单。Rustup 是一个命令行应用,能够下载并在不同版本的 Rust 工具链中进行切换 —— 如编译器 rustc和标准库,该应用所支持的平台数量不少。事实上,rustc 本身就支持大约 56 个平台,而 rustup 实际上能够为其中 14 个平台管理编译器,为 30 个平台管理标准库。

相关概念

channel

Rust 发布在三个不同的 channel 上:stable,beta 和 nightly,其实就是三种不同的版本

  • stable — Rust 的稳定版本,每 6 周发布一次。
  • beta — Rust 的公开测试版本,将是下一个 stable 版本
  • nightly — 每天更新,包含一些实验性的新特性

toolchain

工具链的标准命名格式:

1
2
3
4
5
<channel>[-<date>][-<host>]

<channel> = stable|beta|nightly|<version>
<date> = YYYY-MM-DD
<host> = <target-triple>

工具链默认被安装在 RUSTUP_HOME (Unix系统:~/.rustup ,Windows系统:%USERPROFILE%/.rustup)目录下。

components

工具链由若干组件构成,通过 rustup component list 命令可以查看所有可用和已经安装的组件。

Rustup 默认安装的组件有:

  • rustc — Rust 编译器
  • rust-std — Rust 标准库
  • cargo — 包管理和构建工具
  • rust-docs — Rust 文档
  • rustfmt — 用来格式化 Rust 源代码
  • clippy — Rust 的代码检查工具

profile

不同的 profile 包含不同的组件,安装 rustup 时有三种 profile 可选:

  • minimal — 包含 rustc、rust-std、cargo
  • default — 包含 rustc、rust-std、cargo、rust-docs、rustfmt、clippy
  • complete — 包含所有组件

可以使用 rustup set profile 命令修改 profile,比如:rustup set profile minimal

macOS & linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

/Users/shenxianghong/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

/Users/shenxianghong/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

/Users/shenxianghong/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

/Users/shenxianghong/.profile
/Users/shenxianghong/.zshenv

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


default host triple: aarch64-apple-darwin
default toolchain: stable (default)
profile: default
modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'default'
info: default host triple is aarch64-apple-darwin
info: syncing channel updates for 'stable-aarch64-apple-darwin'
686.2 KiB / 686.2 KiB (100 %) 527.5 KiB/s in 1s ETA: 0s
info: latest update on 2021-12-02, rust version 1.57.0 (f1edd0429 2021-11-29)
info: downloading component 'cargo'
3.7 MiB / 3.7 MiB (100 %) 1.2 MiB/s in 1s ETA: 0s
info: downloading component 'clippy'
info: downloading component 'rust-std'
23.1 MiB / 23.1 MiB (100 %) 4.0 MiB/s in 6s ETA: 0s
info: downloading component 'rustc'
59.4 MiB / 59.4 MiB (100 %) 4.5 MiB/s in 15s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-std'
23.1 MiB / 23.1 MiB (100 %) 19.4 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
59.4 MiB / 59.4 MiB (100 %) 21.9 MiB/s in 2s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-aarch64-apple-darwin'

stable-aarch64-apple-darwin installed - rustc 1.57.0 (f1edd0429 2021-11-29)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env

更新 Rust

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ rustup update
info: syncing channel updates for 'stable-aarch64-apple-darwin'
info: latest update on 2021-12-02, rust version 1.57.0 (f1edd0429 2021-11-29)
info: downloading component 'rust-src'
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-std'
23.1 MiB / 23.1 MiB (100 %) 11.6 MiB/s in 2s ETA: 0s
info: downloading component 'rustc'
59.4 MiB / 59.4 MiB (100 %) 12.6 MiB/s in 4s ETA: 0s
info: downloading component 'rustfmt'
info: removing previous version of component 'rust-src'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'rust-src'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-std'
23.1 MiB / 23.1 MiB (100 %) 18.8 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
59.4 MiB / 59.4 MiB (100 %) 21.3 MiB/s in 2s ETA: 0s
info: installing component 'rustfmt'
info: checking for self-updates

stable-aarch64-apple-darwin updated - rustc 1.57.0 (f1edd0429 2021-11-29) (from rustc 1.53.0 (53cb7b09b 2021-06-17))

info: cleaning up downloads & tmp directories

卸载 Rust

1
2
3
4
5
6
7
8
9
10
11
12
$ rustup self uninstall
Thanks for hacking in Rust!

This will uninstall all Rust toolchains and data, and remove
$HOME/.cargo/bin from your PATH environment variable.

Continue? (y/N) y

info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled

安装校验

1
2
$ rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)

离线文档

即 rust-docs 工具。

1
$ rustup doc

rustup doc 在 apple m1 架构下,文件不存在,无法打开,报错为

1
2
3
error: couldn't open browser: command 'open' did not execute successfully; exit status: 1
command stderr:
The file /Users/shenxianghong/.rustup/toolchains/stable-aarch64-apple-darwin/share/doc/rust/html/index.html does not exist.

Workaround

1
2
$ rustup toolchain install stable-x86_64-apple-darwin
$ rustup doc --toolchain=stable-x86_64-apple-darwin

https://github.com/rust-lang/rustup/issues/2692

示例

文件标准

  • 程序文件后缀名:rs
  • 文件命名规范: hello_world.rs(snake case)

编译与运行

编译

rustc 适合简单的 Rust 程序编译,即 rustc <file>

1
$ rustc main.rs

编译成功后,会生成一个二进制文件,在 Windows 上还会生成一个 .pdb 文件,里面包含调试信息。

类似于 Golang,当待编译的 Rust 程序文件中没有入口函数时,会编译报错

1
2
3
4
5
6
7
8
9
10
11
12
$ rustc rust.rs
error[E0601]: `main` function not found in crate `rust`
--> rust.rs:1:1
|
1 | / fn test() {
2 | | println!("test")
3 | | }
| |_^ consider adding a `main` function to `rust.rs`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0601`.

运行

类似于 Golang,Rust 是 ahead-of-time 编译的语言,可执行文件的运行不依赖于 Rust 环境。

Windows

1
$ .\main.exe

Linux & MacOS

1
$ ./main

简单剖析

1
2
3
fn main() {
println!("Hello World");
}
  • 定义函数使用 fn 关键字,main 函数的作用为每个 Rust 可执行程序最先运行的代码
  • Rust 的缩进是 4 个空格,而不是 tab
  • println! 是一个 Rust macro(宏),如果是函数的话,就没有 !
  • 代码行以 ; 结尾,表示表达式结束,关于表达式和语句的后续会提到
Author

Shen Xianghong

Posted on

2022-01-02

Updated on

2023-06-19

Licensed under